package Servlet;

import Bean.RespContent;
import Server.*;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.UUID;

@WebServlet(name = "Servlet.LoginServlet", value = "/Servlet.LoginServlet")
public class LoginServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        doPost( request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name = request.getParameter("user");
        String psd = request.getParameter("pwd");
        System.out.println("用户名: "+ name+"  密码:"+psd);
        Userservlet uc=new Userservletimpl();
//         RespContent respContent=new  RespContent();
//          Gson gson=new Gson();
        boolean s= false;
        try {
            s = uc.Userservletimpl(name,psd);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (s==true){
//            respContent.setCode(20);
//            respContent.setMsg("恭喜登陆成功");
            String  uuid = UUID.randomUUID().toString();
            ServletContext servletContext = request.getServletContext();
            servletContext.setAttribute("tokeb",uuid);
            Cookie cookie = new Cookie("token", uuid);

            //cookie设置有效时长
            cookie.setMaxAge(1000);
            response.addCookie(cookie);
                response.sendRedirect("/login/login/succ.jsp");
            }else {
//            respContent.setCode(30);
//            respContent.setMsg("用户名或密码不正确，请重新输入!");
             response.sendRedirect("/login/login/loginFail.html");
            }
//        response.getWriter().println(gson.toJson(respContent));
   }
}
